<template>
  <div class="xtx-goods-page">
    <div class="container" v-if="goods.categories">
      <!-- 面包屑 -->
      <XtxBread>
        <XtxBreadItem to="/">首页</XtxBreadItem>
        <XtxBreadItem :to="'/category/' + goods.categories[1].id">{{
          goods.categories[1].name
        }}</XtxBreadItem>
        <XtxBreadItem :to="`/category/sub/${goods.categories[0].id}`">{{
          goods.categories[0].name
        }}</XtxBreadItem>
        <XtxBreadItem to="/">{{ goods.name }}</XtxBreadItem>
      </XtxBread>
      <!-- 商品信息 -->
      <div class="goods-info">
        <!-- 图片预览区 -->
        <div class="media">
          <XtxImageView :imageList="goods.mainPictures" />
          <!-- 统计数量 -->
          <goods-sale :goods="goods" />
        </div>
        <!-- 商品信息区 -->
        <div class="spec">
          <!-- 商品信息 -->
          <goods-info :goods="goods" />
          <!-- 商品详情 -->
          <goods-sku :goods="goods" @change="handleChange" />
          <!-- vue2:  :value="count"
              子组件里接收: props (父=> 子)
              this.$emit('input', 更新的值)   (子=> 父)
              v-model自动帮咱们注册了 @input事件, 会自动更新值,赋值给绑定的变量
          -->
          <!-- :modelValue="count"
                子组件通过props去接收 ( 父=> 子)
                子组件里  context.emit('update:modelValue', 更新的值)
                v-model 自动注册了 @update:modelValue事件, 自动更新值,赋值给绑定变量
                少写了@自定义事件
          -->
          <XtxNumbox :max="countMax" v-model="count" />
          <!-- 按钮组件 -->
          <XtxButton type="primary" style="margin-top: 20px" @click="inserCart">
            加入购物车
          </XtxButton>
        </div>
      </div>
      <!-- 商品详情 -->
      <div class="goods-footer">
        <div class="goods-article">
          <!-- 商品+评价 -->
          <div class="goods-tabs">
            <!-- 商品详情 -->
            <GoodsDetail :goods="goods" />
          </div>
        </div>
        <!-- 24热榜+专题推荐 -->
        <div class="goods-aside">
          <goods-hot :type="1" />
          <goods-hot :type="2" />
          <goods-hot :type="3" />
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { reqFindGoods } from '@/api/goods'
import { useRoute } from 'vue-router'
import { computed, ref } from 'vue'
import GoodsInfo from './components/goods-info'
import GoodsSale from './components/goods-sale'
import GoodsSku from './components/goods-sku'
import GoodsDetail from './components/goods-detail'
import { message } from '@/components/Message/index'
import GoodsHot from './components/goods-hot'
import { useStore } from 'vuex'
export default {
  name: 'XtxGoodsPage',
  components: {
    GoodsInfo,
    GoodsSale,
    GoodsSku,
    GoodsDetail,
    GoodsHot
  },
  setup () {
    // vue2 this.$router.query
    const route = useRoute()
    const goods = ref({})
    const count = ref(1)
    const store = useStore()
    const currSku = ref({})
    // const skuId = ref('skuId')
    // console.log('route', route)
    const findGoods = async () => {
      const { result } = await reqFindGoods(route.params.id)
      // console.log(result)
      goods.value = result
    }
    findGoods()
    // console.log(goods)
    // 当选择不同的sku 组件所返回的结果
    const handleChange = (sku) => {
      // console.log('sku====', sku)
      // 如果选中了独一的sku，那么商品信息页将展示对应sku商品的价格名字，及库存
      if (sku.skuId) {
        goods.value.price = sku.price
        goods.value.oldPrice = sku.oldPrice
        goods.value.inventory = sku.inventory
      }
      currSku.value = sku
    }
    // 设置阶段属性，计算库存量
    const countMax = computed(() => {
      return goods.value.inventory > 0 ? goods.value.inventory : 1
    })
    const inserCart = async () => {
      if (!currSku.value.skuId) {
        return message({
          type: 'error',
          text: '请选择完整规格'
        })
      }
      if (count.value > currSku.value.inventory) {
        return message({
          type: 'error',
          text: '当前库存量不足'
        })
      }
      await store.dispatch('cart/inserCartAPI', {
        id: goods.value.id,
        name: goods.value.name,
        picture: goods.value.mainPictures[0],
        skuId: currSku.value.skuId,
        price: currSku.value.oldPrice,
        nowPrice: currSku.value.price,
        attrsText: currSku.value.specsText,
        stock: currSku.value.inventory,
        selected: true, // 选中
        isEffective: true, // 是否有效
        count: count.value // 商品数量
      })
      message({
        type: 'success',
        text: '加入购物车成功'
      })
      // console.log(store.state.cart.list)
    }
    return {
      goods,
      handleChange,
      countMax,
      count,
      inserCart
    }
  }
}
</script>

<style scoped lang='less'>
.goods-info {
  min-height: 600px;
  background: #fff;
  display: flex;
  .media {
    width: 580px;
    height: 600px;
    padding: 30px 50px;
  }
  .spec {
    flex: 1;
    padding: 30px 30px 30px 0;
  }
}
.goods-footer {
  display: flex;
  margin-top: 20px;
  .goods-article {
    width: 940px;
    margin-right: 20px;
  }
  .goods-aside {
    width: 280px;
    min-height: 1000px;
  }
}
.goods-tabs {
  min-height: 600px;
  background: #fff;
}
.goods-warn {
  min-height: 600px;
  background: #fff;
  margin-top: 20px;
}
</style>
